﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace H2.NET.Web.Workflow.Helpers
{
    public static class UltimusHelper
    {
        /// <summary>
        /// 解析成Url
        /// </summary>
        /// <param name="helpUrl">配置的helpUrl</param>
        /// <returns>返回解析后javascript's onclick方法</returns>
        public static string ResolveUrl(string helpUrl, string id, string title)
        {
            helpUrl = helpUrl.ToLower();
            string result = string.Empty;

            if (!string.IsNullOrEmpty(helpUrl))
            {
                string head = helpUrl.Substring(0, helpUrl.IndexOf(':'));

                string url = helpUrl.Substring(helpUrl.LastIndexOf(':') + 1, helpUrl.Length - (helpUrl.LastIndexOf(':') + 1));

                if (url.StartsWith("../"))
                {
                    url = url.Replace("../", "");
                }
                if (head.Contains("form"))
                {
                    result = "winOpenFullScreen('" + url + "?TaskID=" + id + "')";
                }
                else if (head.Contains("dwz_dialog"))
                {
                    // dwz_dialog_600*400:

                    string size = head.Substring(head.LastIndexOf('_') + 1, head.Length - (head.LastIndexOf('_') + 1));
                    string width = size.Split('*')[0];
                    string height = size.Split('*')[1];
                    result = "return openDialog('" + title.Trim() + "', '" + url + "?TaskID=" + id + "', " + width + ", " + height + ")";
                }
                else if (head.Contains("dwz_navtab"))
                {
                    result = "return openNavTab('" + title.Trim() + "', '" + url + "?TaskID=" + id + "', 'PROCESS" + id + "')";
                }
            }
            else
            {
                result = "winOpenFullScreen('openform.aspx')";
            }

            return result;
        }
        public static string ResolveUrl(string helpUrl, Dictionary<string, string> dic, string title)
        {
            helpUrl = helpUrl.ToLower();
            string result = string.Empty;

            if (!string.IsNullOrEmpty(helpUrl))
            {
                string parms = string.Empty;
                foreach (KeyValuePair<string, string> pair in dic)
                {
                    parms += (string.IsNullOrEmpty(parms) ? "?" : "&") + pair.Key + "=" + pair.Value;
                }

                string head = helpUrl.Substring(0, helpUrl.IndexOf(':'));

                string url = helpUrl.Substring(helpUrl.LastIndexOf(':') + 1, helpUrl.Length - (helpUrl.LastIndexOf(':') + 1));
                if (url.StartsWith("../"))
                {
                    url = url.Replace("../", "");
                }
                if (head.Contains("form"))
                {
                    result = "winOpenFullScreen('" + url + parms + "')";
                }
                else if (head.Contains("dwz_dialog"))
                {
                    // dwz_dialog_600*400:

                    string size = head.Substring(head.LastIndexOf('_') + 1, head.Length - (head.LastIndexOf('_') + 1));
                    string width = size.Split('*')[0];
                    string height = size.Split('*')[1];
                    result = "return openDialog('" + title + "', '" + url + parms + "', " + width + ", " + height + ")";
                }
                else if (head.Contains("dwz_navtab"))
                {
                    result = "return openNavTab('" + title + "', '" + url + parms + "', 'PROCESS" + parms + "')";
                }
            }
            else
            {
                result = "winOpenFullScreen('openform.aspx')";
            }

            return result;
        }

        /// <summary>
        /// 解析状态图片
        /// </summary>
        /// <param name="statue"></param>
        /// <returns></returns>
        public static string ResolveStatusImg(string status)
        {
            string statusImg = "frm_initiate_blank.gif";

            if (status.Trim() == "2")
            {
                statusImg = "frm_initiate_disabled.gif";
            }
            else if (status.Trim() == "1")
            {
                statusImg = "frm_initiate_initiate.gif";
            }

            return statusImg;
        }
    }
}